package com.sx.zongzhi.adapter;


import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sx.zongzhi.R;
import com.sx.zongzhi.activity.AppealActivity;
import com.sx.zongzhi.activity.AppealDealActivity;
import com.sx.zongzhi.activity.AppealProcessActivity;
import com.sx.zongzhi.bean.AppealBean;
import com.sx.zongzhi.utils.Global;
import com.sx.zongzhi.widget.CustomDialog;

import java.util.List;

import static com.sx.zongzhi.R.id.tv_Deal;

/**
 * Created by XZY on 2017/11/23.
 * 诉求服务
 */

public class AppealAdapter extends BaseQuickAdapter<AppealBean.RowsBean,BaseViewHolder> {

    private Context context;

    public
    AppealAdapter(Context context, @LayoutRes int layoutResId, @Nullable List<AppealBean.RowsBean> data) {
        super(layoutResId, data);
        this.context=context;
    }

    @Override
    protected void convert(BaseViewHolder helper, AppealBean.RowsBean item) {
        helper.setText(R.id.tv_Name,item.getSourceUserName());
        helper.getView(R.id.tv_Type).setVisibility(View.GONE);
        helper.setText(R.id.tv_Time,item.getOccurrenceTime());
        helper.setText(R.id.tv_Title,item.getName());
        helper.setText(R.id.tv_EventType,item.getAppealType()!=null?
                item.getAppealType().getDomainName():"");
        helper.setText(R.id.tv_Content,item.getDescription());
        TextView deal=helper.getView(tv_Deal);
        deal.setText( item.isBeAccepted() ? "受理":"办理");
        deal.setOnClickListener(new MyListener(item));
        TextView more= helper.getView(R.id.tv_More);
        if (item.getSourcesWay()==50) {
            deal.setVisibility(View.GONE);
            more.setVisibility(View.GONE);
        }else {
            if (item.getAppealState() == 50) {//待办
                deal.setVisibility(View.VISIBLE);
                more.setVisibility(View.VISIBLE);
            } else {
                deal.setVisibility(View.GONE);
                more.setVisibility(View.GONE);
            }
            if (item.getAppealState() == 90) {//历史遗留
                helper.getView(R.id.tv_DealProcess).setVisibility(View.GONE);
            }
        }
        helper.getView(R.id.tv_DealProcess).setOnClickListener(new MyListener(item));//处理过程
        more.setOnClickListener(new MyListener(item));
    }

    private class MyListener implements View.OnClickListener{

        private AppealBean.RowsBean rowsBean;

        public MyListener(AppealBean.RowsBean rowsBean) {
            this.rowsBean=rowsBean;
        }
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case tv_Deal:
                    if (rowsBean.isBeAccepted()){//受理
                        createAccept(rowsBean.getAppealId(),rowsBean.getStepRecordId());
                    }else {//办理
                        context.startActivity(new Intent(context, AppealDealActivity.class)
                                .putExtra("appealId",rowsBean.getAppealId())
                                .putExtra("stepRecordId",rowsBean.getStepRecordId())
                                .putExtra(Global.ACTIVITY_FRAGMENT,"AppealActivity"));
                    }
                    break;
                case R.id.tv_DealProcess:
                    context.startActivity(new Intent(context, AppealProcessActivity.class)
                            .putExtra("appealId",rowsBean.getAppealId()));
                    break;
                case R.id.tv_More:
                    ((AppealActivity)context).showDealPop(rowsBean);
                    break;
            }
        }
    }


    /**
     * 提示受理的弹窗
     * @param appealId 诉求事件id
     *  @param stepRecordId 事件步骤id
     */
    private void createAccept(final String appealId,final String stepRecordId) {
        CustomDialog.Builder builder=new CustomDialog.Builder(context);
        CustomDialog dialog=builder.setMessage("确定受理？").
                setBackButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).setConfirmButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                ((AppealActivity)context).acceptDispute(appealId,stepRecordId,"2049","");
                dialog.dismiss();
            }
        }).create();
        dialog.show();
    }

}
